/*
 ============================================================================
 Name        : task_serv.c
 Author      : dogguo
 Time        : 2016/04/30 15:47
 Version     : V0.0.1
 Copyright   : WifiShare Cooperation
 Description : WL APP
 ============================================================================
 */

#include <stdio.h>
#include "lib/util/net_http.h"
#include "app/gconstant.h"
#include "lib/cjson/cJSON.h"


int get_vpncrt_url(char* vpncrt_url, char* param) {
    printf("get_vpncrt_url <<<--- \n");

    // http get request
    char* recv_buf = (char*)malloc(sizeof(char*)*512);
    if(recv_buf == NULL ) {
        printf("get_vpncrt_url <<<--- result_buf malloc failed ! \n");
        return 0;
    }
    int recv_len = net_http_get(URL_SERV_GET_DEVICE_VPNCRT, param, 5000, recv_buf);
    printf("get_vpncrt_url <<<--- recv_len = %d \n", recv_len);
    printf("get_vpncrt_url <<<--- recv_buf =  %s \n", recv_buf);
    if(recv_len == -1) {
        printf("get_vpncrt_url <<<--- net_http_get failed ! \n");
        return 0;
    }

    // parse root node
    cJSON* recv_cj = cJSON_Parse(recv_buf);
    if(recv_cj == NULL ) {
        printf("get_vpncrt_url <<<--- recv_cj is NULL ! \n");
        return 0;
    }

    // parse root node retCode
    cJSON* ret_code_cj = cJSON_GetObjectItem(recv_cj,"retCode");
    if(NULL == ret_code_cj) {
        printf("get_vpncrt_url <<<--- result_cj is NULL ! \n");
        return 0;
    }
    int ret_code = ret_code_cj->valueint;
    printf("get_vpncrt_url <<<--- ret_code = %d \n", ret_code);
    if(ret_code == 0) {
        printf("get_vpncrt_url <<<--- server failed ! \n");
        return 0;
    }

    // parse root node result
    cJSON* result_cj = cJSON_GetObjectItem(recv_cj,"result");
    if(NULL == result_cj) {
        printf("get_vpncrt_url <<<--- result_cj is NULL ! \n");
        return 0;
    }

    // parse root subnode vpncrtUrl
    cJSON* vpncrt_url_cj = cJSON_GetObjectItem(result_cj,"vpncrtUrl");
    if(NULL == vpncrt_url_cj) {
        printf("get_vpncrt_url <<<--- vpncrt_url_cj is NULL ! \n");
        return 0;
    }
    strcpy(vpncrt_url, vpncrt_url_cj->valuestring);
    if(NULL == vpncrt_url) {
        printf("get_vpncrt_url <<<--- vpncrt_url is NULL ! \n");
        return 0;
    }
    printf("get_vpncrt_url <<<--- vpncrt_url = %s \n", vpncrt_url);

    // free tmp memory
    free(recv_buf);

    printf("get_vpncrt_url --->>> \n");
    return 1;
}


/* */
int post_wifi_status(char* param) {
    printf("post_wifi_status <<<--- \n");

    // http post request
    char* recv_buf = (char*)malloc(sizeof(char*)*512);
    if(recv_buf == NULL ) {
        printf("post_wifi_status <<<--- result_buf malloc failed ! \n");
        return 0;
    }
    int recv_len = net_http_post(URL_SERV_POST_WIFI_STATUS, param, 5000, recv_buf);
    printf("post_wifi_status <<<--- recv_len = %d \n", recv_len);
    printf("post_wifi_status <<<--- recv_buf =  %s \n", recv_buf);
    if(recv_len == -1) {
        printf("post_wifi_status <<<--- net_http_get failed ! \n");
        return 0;
    }

    // parse root node
    cJSON* recv_cj = cJSON_Parse(recv_buf);
    if(recv_cj == NULL ) {
        printf("post_wifi_status <<<--- recv_cj is NULL ! \n");
        return 0;
    }

    // parse root node retCode
    cJSON* ret_code_cj = cJSON_GetObjectItem(recv_cj,"retCode");
    if(NULL == ret_code_cj) {
        printf("post_wifi_status <<<--- result_cj is NULL ! \n");
        return 0;
    }
    int ret_code = ret_code_cj->valueint;
    printf("post_wifi_status <<<--- ret_code = %d \n", ret_code);
    if(ret_code == 0) {
        printf("post_wifi_status <<<--- server failed ! \n");
        return 0;
    }

    // free tmp memory
    free(recv_buf);

    printf("post_wifi_status --->>> \n");
    return 1;
}


int post_device_gpm(char* param) {
    printf("post_device_gpm <<<--- \n");

    // http post request
	char* recv_buf = (char*)malloc(sizeof(char*)*512);
	if(recv_buf == NULL ) {
		printf("post_device_gpm <<<--- result_buf malloc failed ! \n");
		return 0;
	}
	int recv_len = net_http_post(URL_SERV_POST_DEVICE_GPM, param, 5000, recv_buf);
	printf("post_device_gpm <<<--- recv_len = %d \n", recv_len);
	printf("post_device_gpm <<<--- recv_buf =  %s \n", recv_buf);
	if(recv_len == -1) {
		printf("post_device_gpm <<<--- net_http_get failed ! \n");
		return 0;
	}

	// parse root node
	cJSON* recv_cj = cJSON_Parse(recv_buf);
	if(recv_cj == NULL ) {
		printf("post_device_gpm <<<--- recv_cj is NULL ! \n");
		return 0;
	}

	// parse root node retCode
	cJSON* ret_code_cj = cJSON_GetObjectItem(recv_cj,"retCode");
	if(NULL == ret_code_cj) {
		printf("post_device_gpm <<<--- result_cj is NULL ! \n");
		return 0;
	}
	int ret_code = ret_code_cj->valueint;
	printf("post_device_gpm <<<--- ret_code = %d \n", ret_code);
	if(ret_code == 0) {
		printf("post_device_gpm <<<--- server failed ! \n");
		return 0;
	}

	// free recv_buf memory
	free(recv_buf);

    printf("post_device_gpm --->>> \n");
    return 1;
}

